﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Framework;
using XLua;

namespace Game.Lua.Base
{

    public static class LuaConfig
    {
        [CSharpCallLua] 
        public static List<Type> CSharpCallLua = new List<Type>()
        {
            // typeof(MonoBehaviour),
            typeof(System.Func<bool>),
            typeof(Action<LuaTable,float,float,object>),
            typeof(Action<LuaTable,object>),
            typeof(Action<float,float>),
            // typeof(System.Collections.Stack),
        };

        [LuaCallCSharp()]
        public static List<Type> LuaCallCSharp = new List<Type>()
        {
            typeof(UnityGameEntry),
            typeof(GameEntry),
            // GetFrameworkComponent(),
        };
        [LuaCallCSharp()]
        public static Type[] GetFrameworkComponent()
        {
            
            List<Type> ls = new List<Type>();
            
            
            Assembly assembly = Assembly.Load("Assembly-CSharp");
            Type baseType = typeof(FrameworkComponent);
            
            
            foreach (var type in assembly.GetTypes())
            {
                if (type.IsClass && !type.IsAbstract && type.IsAssignableFrom(baseType))
                {
                    ls.Add(type);
                }
            }
            return ls.ToArray();
        }
        public static List<Type> BlackGenericTypeList = new List<Type>()
        {
            typeof(Span<>),
            typeof(ReadOnlySpan<>),
        };

        private static bool IsBlacklistedGenericType(Type type)
        {
            if (!type.IsGenericType) return false;
            return BlackGenericTypeList.Contains(type.GetGenericTypeDefinition());
        }

        [BlackList] public static Func<MemberInfo, bool> GenericTypeFilter = (memberInfo) =>
        {
            switch (memberInfo)
            {
                case PropertyInfo propertyInfo:
                    return IsBlacklistedGenericType(propertyInfo.PropertyType);

                case ConstructorInfo constructorInfo:
                    return constructorInfo.GetParameters().Any(p => IsBlacklistedGenericType(p.ParameterType));

                case MethodInfo methodInfo:
                    return methodInfo.GetParameters().Any(p => IsBlacklistedGenericType(p.ParameterType));

                default:
                    return false;
            }
        };
        //黑名单
    }

}